<?php
namespace Api\Controller;
use Think\Controller;
/* 手机端首页控制器 */

class MysqlDefaultController extends CommonController {
    public function cate_index(){
        $detail = I('post.');
        $keyword=$detail['keyword'];
        $limit = $detail['limit']? : 10;
		$page = $detail['page']? : 1;
        $model = M('mysql_default_cate');
        $where['isdel']=0;
        $where['stype']=1;
        $where['pid']=0;
        if($keyword) {
            $where['title'] = array('like',"%".$keyword."%");
        }
        $count=$model->where($where)->count();
        $list= $model->where($where)->field('id,title,pid,sht_c')->order('id asc')->page($page, $limit)->select();
        if($list){
            foreach($list as $k=>$val){
                $list[$k]['level']=1;
                $lis5c=$model->where(['pid'=>$val['id'],'is_delete'=>0])->field('id,title,pid,sht_c')->order('id desc')->select();
                $list[$k]['counts']=count($lis5c);
                $list[$k]['child_list']=$lis5c;
                if($lis5c){
                       foreach($lis5c as $k1=>$v1){
                            $list[$k]['child_list'][$k1]['level']=2;
                            $list[$k]['child_list'][$k1]['counts']=count($this->get_child($v1['id']));
                            $list[$k]['child_list'][$k1]['children']=$this->get_child($v1['id']);
                       }
                }

            }
        }
        $data_cc['code'] = 200;
        $data_cc['msg'] = "查询成功";
        $data_cc['data']['count']=$count;
        $data_cc['data']['list']=$list;
        exit(json_encode($data_cc));
    }

    //查询子分类
    function get_child($pid){
        $model = M('mysql_default_cate');
        $lis5c=$model->where(['pid'=>$pid,'is_delete'=>0])->field('id,title,pid,sht_c')->order('id desc')->select();
        return $lis5c;
    }

    //添加分类
    public function add(){
        $mode = M('mysql_default_cate');
        $detail = I('');
        $title = $detail['title'];
        $pid = $detail['pid'];
        $sht_c = $detail['sht_c'];//分类类型：0-上衣;1-下装
        if(empty($title)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = '请填写分类名称';
            exit(json_encode($data_cc));
        }
        

    }
    //编辑

    //删除

    

    //导入Excel
    function import(){
        ini_set('max_execution_time', '0');
        ini_set('memory_limit','1024M');
        $excel_path = I('excel_path');
        $cat_id = I('cat_id');
        $sex = I('sex');
        $type = I('type');
        if(!$excel_path){
            $data_cc['code'] = 0;
            $data_cc['msg'] = 'excel地址必须！';
            exit(json_encode($data_cc));
        }
        if(I('type') == ''){
            $data_cc['code'] = 0;
            $data_cc['msg'] = '模板类型为空';
            exit(json_encode($data_cc));
        }
        if(I('sex') == ''){
            $data_cc['code'] = 0;
            $data_cc['msg'] = '请选择性别';
            exit(json_encode($data_cc));
        }
        if(!I('cat_id')){
            $data_cc['code'] = 0;
            $data_cc['msg'] = '请选择类型';
            exit(json_encode($data_cc));
        }
        if($type == 0){
            //上装
            $model = M('user_mysql_default_list');
        }elseif($type == 1){
            //下装
            $model = M('user_mysql_default_listc');
        }else{
            $data_cc['code'] = 0;
            $data_cc['msg'] = '参数错误';
            exit(json_encode($data_cc));
        }
        $path = UID.'_attn.xlsx';//考勤excel
        file_put_contents($path, file_get_contents($excel_path));
        $exc_data = importExcel($path);
        $model->startTrans();
        trace(count($exc_data),'exc_data');
        foreach($exc_data as $key=>$val){
            $data = [];
            if($key==0){
                continue;
            }
            if($type == 0){
                $data['cat_id'] = $cat_id;
                $data['sex'] = $sex;
                $data['high'] = $val[0];
                $data['weight'] = $val[1];
                $data['size'] = $val[2];
                $data['qiancyi'] = $val[3];
                $data['hzhc'] = $val[4];
                $data['cyxw'] = $val[5];
                $data['zhonyao'] = $val[6];
                $data['chenyiyaow'] = $val[7];
                $data['jiankuan'] = $val[8];
                $data['xiuchang'] = $val[9];
            }else{
                $data['cat_id'] = $cat_id;
                $data['sex'] = $sex;
                $data['high'] = $val[0];
                $data['weight'] = $val[1];
                $data['size'] = $val[2];
                $data['kuchang'] = $val[3];
                $data['cpyaowei'] = $val[4];
                $data['cptunwei'] = $val[5];
                $data['qzhidan'] = $val[6];
                $data['jiaokou'] = $val[7];
                $data['qianlang'] = $val[8];
                $data['houlang'] = $val[9];
                $data['tuiwei'] = $val[10];
                $data['zhotui'] = $val[11];
                $data['menjinc'] = $val[12];
            }
            $data['add_time'] = time();
            $s_wdata['cat_id'] = $cat_id;
            $s_wdata['sex'] = $sex;
            $s_wdata['uid'] = UID;
            $s_wdata['high'] = $val[0];
            $s_wdata['weight'] = $val[1];
            $s_wdata['size'] = $val[2];
            $s_vo = $model->where($s_wdata)->find();
            if($s_vo){
                continue;
            }else{
                $res = $model->add($data);
                if($res===false){
                    $data_cc['code'] = 0;
                    $data_cc['msg'] = '添加数据失败';
                    exit(json_encode($data_cc));
                }
            }
            
            
        }
        $model->commit();
        unlink($path);
        $data_cc['code'] = 200;
        $data_cc['msg'] = '导入成功';
        exit(json_encode($data_cc));
    }
}
